//
//  XGViewController.m
//  XGClockTime
//
//  Created by 小果 on 16/7/22.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "XGViewController.h"

@interface XGViewController ()
/**
 *  表盘
 */
@property (nonatomic, strong) UIImageView *clockView;
/**
 *  小时
 */
@property (nonatomic, weak) CALayer *hour;
@property (nonatomic, weak) UILabel *Hous;
/**
 *  分钟
 */
@property (nonatomic, weak) CALayer *minutes;
@property (nonatomic, weak) UILabel *minu;
/**
 *  秒
 */
@property (nonatomic, weak) CALayer *second;
@property (nonatomic, weak) UILabel *secon;

@property (nonatomic, assign) CGFloat hours;
@property (nonatomic, assign) CGFloat min;
@property (nonatomic, assign) CGFloat sec;

@end


@implementation XGViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BgColor;
    // 创建表盘
    [self createClockView];
    
    // 添加时针
    [self addHour];
    
    // 添加分针
    [self addMinutes];
    
    // 添加秒针
    [self addSecond];
    
    // 添加锚点
    CALayer *layer = [self setupLayerWithBackGroundColor:[UIColor yellowColor] anchorPoint:CGPointMake(0.5, 0.5) layerBounds:CGRectMake(0, 0, 14, 14)];
    layer.borderColor = [UIColor orangeColor].CGColor;
    layer.borderWidth = 2;
    layer.cornerRadius = 7;
    
    // 添加一个定时器
    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateTime)];
    [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    
    [self setupClock];
    
    // 一开始加载就主动刷新秒针
    [self updateTime];
    
}

#pragma mark - 创建表盘
-(void)createClockView{
    
    UIImageView *clock = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
    clock.image = [UIImage imageNamed:@"clock_view"];
    clock.center = self.view.center;
    self.clockView = clock;
    [self.view addSubview:clock];

}

#pragma mark - 创建时、分、秒的控件
-(void)setupClock{
    // 之所以分别创建时针、分针和秒针的三个独立的空间的原因是：如果把三个时间都写在一个控件上，那么在刷新秒针的时候，整个控件为跟着秒针的不断刷新而发生左右轻微的偏移的现象
    UIView *clockV = [[UIView alloc] initWithFrame:CGRectMake((ScreenW - ClockViewW) * 0.5 +5, CGRectGetMaxY(self.clockView.frame) + 40, ClockViewW, ClockViewH)];
    [self.view addSubview:clockV];
    
    // 时针对应的数字标签
    UILabel *Hour = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ClockViewW / 3 , ClockViewH)];
    [self setupTextWithLabe:Hour Size:TextFontSize alignment:NSTextAlignmentCenter];
    self.Hous = Hour;
    [clockV addSubview:Hour];
    
    // 分针对应的数字标签
    UILabel *minu = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(Hour.frame), 0, ClockViewW / 3, ClockViewH)];
    [self setupTextWithLabe:minu Size:TextFontSize alignment:NSTextAlignmentCenter];
    self.minu = minu;
    [clockV addSubview:minu];
    
    // 秒针对应的数字标签
    UILabel *sec = [[UILabel alloc] initWithFrame:CGRectMake( CGRectGetMaxX(minu.frame), 0, ClockViewW / 3, ClockViewH)];
    [self setupTextWithLabe:sec Size:TextFontSize alignment:NSTextAlignmentCenter];
    self.secon = sec;
    [clockV addSubview:sec];
}
-(void)setupTextWithLabe:(UILabel *)labe Size:(CGFloat)size alignment:(NSTextAlignment)alignment{
    
    labe.font = [UIFont boldSystemFontOfSize:size];
    labe.textAlignment = alignment;
}

#pragma mark - 每一秒调用60次
- (void)updateTime{
    // 获取当前的日期
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 获取日历中的组件:秒,分,时,天
    // NSCalendarUnit:获取哪个时间
    NSDateComponents *cmp = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitNanosecond fromDate:[NSDate date]];
    
    // 小时
    self.hours = cmp.hour;
    self.Hous.text = [NSString stringWithFormat:@"%02.0lf :",self.hours];

    // 分钟
    self.min = cmp.minute;
    self.minu.text = [NSString stringWithFormat:@" %02.0lf :",self.min];

    // 秒数
    self.sec = cmp.second;
    self.secon.text = [NSString stringWithFormat:@"%02.0lf",self.sec];
   
    // 纳秒(每秒等于 1000,000,000纳秒)
//    CGFloat nanosecond = cmp.nanosecond;
    
    // 计算秒针旋转的角度 = 当前的秒数 * 每秒秒针转多少度(6) + 当前的纳秒 * 每纳秒秒针转多少度(0.000000001)
    CGFloat secondAngle = RADIAN(self.sec * SECOND_ANGLE);
#warning 以下两行代码可以设置秒针按纳秒的速度去计算转动的度数
//    CGFloat se = ANGLE_2_RADIAN(nanosecond * PER_SECOND_NANOSECOND_ANGLE);
//    secondAngle += se;
    
    // 计算分针旋转的角度 = 当前的分针 * 每分钟分针转多少度(6) + 当前的秒钟 * 每秒分针转多少度(0.1);
    CGFloat minuteAngle = RADIAN(self.min * MINUTE_ANGLE);
    // 每秒钟分针旋转的角度 = 当前的秒钟 * 每秒分针转多少度(0.1)
    CGFloat secondAndMinuteAngel = RADIAN(self.sec * MINUTE_AND_SECOND_ANGLE);
    minuteAngle += secondAndMinuteAngel;
    
    // 计算时针旋转的角度 = 当前的小时 * 每小时时针转多少度(30) + 当前的分钟 * 每分钟时针转多少度(0.5) + 当前的秒钟 * 每秒钟时针转多少度(0.5 / 60)
    CGFloat hourAngle = RADIAN(self.hours * HOUR_ANGLE);
    // 每隔一分钟,时针也需要转一定的角度
    CGFloat minuteAndHourAngle = RADIAN(self.min * HOUR_MINUTE_ANGLE);
    hourAngle += minuteAndHourAngle;
    // 每秒移动的时钟度数 = 当前的秒钟 * 每秒钟时针转多少度(0.5 / 60)
    CGFloat secondAndHourAngle = RADIAN(self.sec * HOUR_SECOND_ANGLE);
    hourAngle += secondAndHourAngle;
    
    
    // 旋转时针
    self.hour.transform = CATransform3DMakeRotation(hourAngle, 0, 0, 1);
    // 旋转分针
    self.minutes.transform = CATransform3DMakeRotation(minuteAngle, 0, 0, 1);
    // 旋转秒针
    self.second.transform = CATransform3DMakeRotation(secondAngle, 0, 0, 1);
}

#pragma mark - 添加时针
- (void)addHour{
    _hour = [self setupLayerWithBackGroundColor:[UIColor brownColor] anchorPoint:CGPointMake(0.5, 0.75) layerBounds:CGRectMake(0, 0, 6, 100)];
}
#pragma mark - 添加分针
- (void)addMinutes{
    _minutes = [self setupLayerWithBackGroundColor:[UIColor blueColor] anchorPoint:CGPointMake(0.5, 0.8) layerBounds:CGRectMake(0, 0, 5, 120)];
}
#pragma mark - 添加秒针
- (void)addSecond{
    _second = [self setupLayerWithBackGroundColor:[UIColor redColor] anchorPoint:CGPointMake(0.5, 0.85) layerBounds:CGRectMake(0, 0, 4, 140)];
}


#pragma mark - 自定义layer并设置颜色 位置 尺寸
- (CALayer *)setupLayerWithBackGroundColor:(UIColor *)bgColor anchorPoint:(CGPoint)anchorPoint layerBounds:(CGRect)bounds{
    
    CALayer *layer = [CALayer layer];
    layer.backgroundColor = bgColor.CGColor;
    layer.anchorPoint = anchorPoint;
    layer.position = CGPointMake(self.clockView.frame.size.width * 0.5, self.clockView.frame.size.width * 0.5 - 10);
    layer.cornerRadius = 5;
    layer.masksToBounds = YES;
    layer.bounds = bounds;
    [self.clockView.layer addSublayer:layer];
    
    return layer;
}




@end
